<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="700" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="类型" prop="type">
				 <el-radio-group v-model="form.type">
						<el-radio :label="0">Api获取数据</el-radio>
						<el-radio :label="1">Api提交数据</el-radio>
				 </el-radio-group>
			</el-form-item>
			<el-form-item label="请求" prop="methods">
				      <el-radio-group v-model="form.methods">
							<el-radio :label="1">GET</el-radio>
							<el-radio :label="2">POST</el-radio>
				      </el-radio-group>
			</el-form-item>
			<el-form-item label="Api Url" prop="api_url">
				<el-input v-model="form.api_url"></el-input>
			</el-form-item>
			<el-form-item label="关联模块" prop="page_module_ids">
				      <el-select
				         v-model="form.page_module_ids"
				         multiple
						 clearable
						 filterable
				         placeholder="请选择模块"
						 style="width:100%"

				       >
				         <el-option
				           v-for="(item,index) in modules"
				           :key="index"
				           :label="item.marks + '【' + item.class + '/' + item.method + '】'"
				           :value="item.id"
				         />
				       </el-select>

			</el-form-item>
			<el-form-item label="备注" prop="remarks">
				<el-input v-model="form.remarks"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				modules:[],
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					type:1,
					methods:2
                },
				//验证规则
				rules: {
					type: [
						{required: true, message: '请输入备注'}
					],
					class: [
						{required: true, message: '请输入模块'}
					],
					method: [
						{required: true, message: '请输入方法名'}
					],
				}
			}
		},
		mounted() {
			 this.getmodules()
		},
		methods: {
			async getmodules(){
				var res = await this.$API.system.universal.list.get({'nopage':1},'/system/module');
				if(res.code == 200){
					 this.modules = res.data
				}
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
                        var res = await this.$API.system.universal.handle.post(this.form,'/system/pageModule_save');
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				console.log(data,"xxxxxxxxx")
				this.form = data
			}
		}
	}
</script>

<style>
</style>
